<template>
  <app-list>
    <template #search>
      <div class="setting-title">来源管理</div>
    </template>
    <template #action>
      <el-button type="primary" class="hovepcvicecolor" icon="plus" @click="handleadd()">新增来源</el-button>
    </template>
    <el-table stripe :data="pageData.list" v-loading="pageData.loading">
      <el-table-column label="来源名称" prop="name" show-overflow-tooltip />
      <el-table-column label="联系方式" prop="contact" show-overflow-tooltip />
      <el-table-column label="备注" prop="remark" show-overflow-tooltip />
      <el-table-column label="创建时间" prop="created_at" show-overflow-tooltip>
        <template #default="scope">
          <div>{{ scope.row.created_at }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <div style="display: flex">
            <div class="csspointer" @click="handleEdit(scope.row)">编辑</div>
            <div class="csspointer" style="margin-left: 12px" @click.native.prevent="deleteRow(scope.row)">
              删除
            </div>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <template #footerRight>
      <el-pagination
        @size-change="pageActions.handleSizeChange"
        @current-change="pageActions.handlePageChange"
        v-model:current-page="pageData.page"
        v-model:page-size="pageData.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageData.total"
        background
      />
    </template>

    <AddItem v-model="dialog.edit.show" :data="dialog.edit.data" :parentdata="parentdata" @change="changeform()" />
  </app-list>
</template>

<script setup>
import { http } from '@/utils/http'
import { ElMessage, ElMessageBox } from 'element-plus'
import AddItem from '../AddItem.vue'

const { pageData, pageActions, dialog } = usePage(
  () => {
    const query = {
      page: pageData.page,
      page_size: pageData.pageSize,
      ...pageData.query,
    }

    return http.get('/source/source_list', {
      params: query,
    })
  },
  {
    query: {},
  }
)

const parentdata = ref({
  value: 'source',
  label: '新增来源'
})
const timetrans = inject('timetrans');

// 新增
const handleadd = () => {
  parentdata.value.status = 'create'
  parentdata.value.label = '新增来源'
  pageActions.handleAdd()
}

// 编辑
const handleEdit = (row) => {
  parentdata.value.status = 'update'
  parentdata.value.label = '编辑来源'
  pageActions.handleEdit(row)
}

// 删除
const deleteRow = row => {
  ElMessageBox.confirm('是否删除?', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      http.delete(`/source/source_delete?id=${row.id}`).then(res => {
      ElMessage.success('删除成功')
      pageActions.refresh(true)
    })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消',
      })
    })
}

const changeform = () => {
    pageActions.refresh(true)
}
</script>

<style lang="scss" scoped>

</style>